// pages/classify/classify.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toggle: true,
    partData: {
      imgUrls: '',
      name: '',
      inventory: 0,
      price: '',
      goods_id: '',
      detail:false
    },
    stairNav: [], //一级分类列表
    secondLevelNav: [], //二级分类列表
    goodsList: [], //商品
    stairIndex: 0, //一级分类索引id
    secondLeveIndex: 0, //二级分类索引id
    showGoods: true, //商品是否为空
    noClass: true,
    noData: false,
    shade: false,
    hiddenLoading: true,
    stairId: null, //一级分类id
    secondLeveId: null, //二级分类id
    // rightCenter: true, //二级菜单是否为空
    // type: 0, //渠道会员type =1
    // jrdshow: true,
    // crdshow: false,
    // navisshow: false,
    // iscrdtype: true,
    
  },
  // 点击一级列表
  stairClick(e) {
    const that = this;
    that.setData({
      stairId: e.currentTarget.dataset.catid,
      stairIndex: e.currentTarget.dataset.index,
      secondLeveIndex: 0
    })
    let json = {
      cat_id: that.data.stairId
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.classifyTwo, jsonData).then(res => {
      if (res.status == 1) {
        if (res.result == '') {
          return false;
        } else {
          that.setData({
            secondLevelNav: res.result,
            secondLeveId: res.result[0].cat_id,
          })
          let json = {
            cat_id1: that.data.stairId,
            cat_id2: that.data.secondLeveId
          }
          let jsonData = app.utils.getSignData(json);
          return app.utils.post(app.api.reqUrls.classifyGoodsList, jsonData)
        }
      } else {
        throw res.msg
      }
    }).then((res) => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        // 判断商品是否为空
        that.setData({
          showGoods: res.result && res.result.length > 0
        })
        res.result.forEach(item => {
          item.sales_sum = '销量' + ' ' + item.sales_sum;
        });
        that.setData({
          goodsList: app.getImg(res.result)
        })
      }
    }).catch(err => {
      app.showInfo(err.toString());
      that.setData({
        hiddenLoading: true
      })
    })
  },
  // 点击二级列表
  secondLeveClick(e) {
    const that = this;
    that.setData({
      secondLeveId: e.currentTarget.dataset.catid,
      secondLeveIndex: e.currentTarget.dataset.index
    })
    let json = {
      cat_id1: that.data.stairId,
      cat_id2: that.data.secondLeveId
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.classifyGoodsList, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        // 判断商品是否为空
        that.setData({
          showGoods: res.result && res.result.length > 0
        })
        res.result.forEach(item => {
          item.sales_sum = '销量' + ' ' + item.sales_sum
        });
        that.setData({
          goodsList: app.getImg(res.result)
        })
      }
    }).catch(err => {
      app.showInfo(err.toString())
      that.setData({
        hiddenLoading: true
      })
    })
  },
  // 跳到商品详情
  skipDetails(e) {
    wx.navigateTo({
      url: `/page/detail/pages/details/details?id=${e.currentTarget.dataset.goodid}`
    })
  },
  // 跳到搜索页
  toSearch() {
    wx.navigateTo({
      url: '/page/detail/pages/search/search',
    })
  },
  // 添加到购物车
  // addToCart(e) {
  //   const that = this;
  //   app.checkUserInfoAuthorizationCart().then(() => {
  //     let goodsid = e.currentTarget.dataset.goodsid;
  //     let token = app.globalData.token;
  //     let json = {
  //       token,
  //       goods_num: 1,
  //       goods_id: goodsid,
  //     }
  //     let jsonData = app.utils.getSignData(json);
  //     that.setData({
  //       hiddenLoading: false
  //     })
  //     app.utils.post(app.api.reqUrls.addCart, jsonData).then(res => {
  //       that.setData({
  //         hiddenLoading: true
  //       })
  //       if (res.status == 1) {
  //         // 添加成功
  //         wx.showToast({
  //           title: '已添加到购物车',
  //           icon: 'success',
  //           duration: 1000
  //         })
  //         app.getCartCount();
  //       } else {
  //         throw res.msg;
  //       }
  //     }).catch(err => {
  //       app.showInfo(err.toString())
  //       that.setData({
  //         hiddenLoading: true
  //       })
  //     })
  //   })
  // },
  // 下拉刷新
  refresh() {
    const that = this;

    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
    let json = {}
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.classifyOne, jsonData).then(res => {
      if (res.status == 1) {
        if (res.result == '') {
          that.setData({
            noClass: false
          })
          throw '分类为空'
        }
        let stairId = that.data.stairId || res.result[0].cat_id
        that.setData({
          noClass: true,
          stairNav: res.result,
          stairId: stairId
        })
        let json = {
          cat_id: stairId
        }
        let jsonData = app.utils.getSignData(json);
        return app.utils.post(app.api.reqUrls.classifyTwo, jsonData)
      } else {
        that.setData({
          noClass: false
        })
        throw res.msg
      }
      // 获取二级分类
    }).then((res) => {
      if (res.status == 1) {
        if (that.data.secondLeveId == null && res.result[0].cat_id == '') {
          // return -999
          throw '-999'
        }
        let secondLeveId = that.data.secondLeveId || res.result[0].cat_id
        that.setData({
          secondLevelNav: res.result,
          secondLeveId: secondLeveId
        })
        let json = {
          cat_id1: that.data.stairId,
          cat_id2: secondLeveId
        }
        let jsonData = app.utils.getSignData(json);
        that.setData({
          hiddenLoading: false
        })
        return app.utils.post(app.api.reqUrls.classifyGoodsList, jsonData)
      } else {
        throw res.msg
      }
      // 获取商品列表
    }).then((res) => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        // 判断商品是否为空
        that.setData({
          showGoods: res.result && res.result.length > 0
        })
        res.result.forEach(item => {
          item.sales_sum = '销量' + ' ' + item.sales_sum
        });
        that.setData({
          goodsList: app.getImg(res.result)
        })
        // app.getCartCount();
      // } else {
      //   throw res.msg
      }
    }).catch(err => {
      // console.log(err,"8888888888");
      app.showInfo(err.toString())
      that.setData({
        hiddenLoading: true
      })
      
    })
  },
// 点击添加购物车
addShowCart(e) {
  const that = this;
  console.log(e, 78787)
  let goods = e.currentTarget.dataset.goods
  that.setData({
    toggle: false,
    'partData.imgUrls': goods.img,
    'partData.name': goods.goods_name,
    'partData.inventory': goods.store_count,
    'partData.price': goods.shop_price,
    'partData.goods_id': goods.goods_id,
  })
},
// 添加到购物车
addToCart(e) {
  app.checkUserInfoAuthorizationCart().then(() => {
    const that = this;
    let token = app.globalData.token;
    let json = {
      token,
      goods_num: e.detail,
      goods_id: that.data.partData.goods_id,
    }
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.addCart, jsonData).then(res => {
      if (res.status == 1) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000
        })
        app.getCartCount();
        app.cartCountCallback = (res) => {
          that.setData({
            cartCount: res
          })
        }
      } else {
        throw res.msg
      }
    }).catch(err => {
      app.showInfo(err.toString())
    })

  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    console.log(app.globalData.classifyIndex,888888)
    that.setData({
      shade: app.globalData.shade,
      stairId: app.globalData.classifyIndex,
      toggle: true,
    })
    app.globalData.classifyIndex = 0
    that.refresh();
    // }
    // 判断是否渠道会员，type == 1是渠道会员
    // app.getInfo(app.globalData.token, () => {
    //   console.log(app.globalData.userInfo)
    //   if (app.globalData.userInfo) {
    //     that.setData({
    //       type: app.globalData.userInfo.type
    //     })
    //   } else {
    // that.setData({
    //   type: 0
    // })
    //   }
    // })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      hiddenLoading: false,
      stairId: '',
      secondLeveId: '',
      secondLeveIndex: 0,
      stairIndex: 0
    })
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.refresh();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    const that = this;
    app.globalData.pos_type = '';
    that.setData({
      toggle: true,
    })
  },

})